<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>学生管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<style>
    .layui-table-cell{
        height:120px;
        line-height: 120px;
    }
</style>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="#">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-inline">
                                <select name="gender" id="applied">
                                    <option value="">请选择</option>
                                    <option value="0">女</option>
                                    <option value="1">男</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <a class="layui-btn" lay-submit="" lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</i>搜索
                            </a>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">
                <i class="layui-icon">&#xe642;</i>编辑
            </a>
            <a class="layui-btn layui-btn-xs data-count-delete" lay-event="delete">
                <i class="layui-icon">&#xe640;</i>删除
            </a>
        </script>

    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'httpClient', 'layer', 'miniTab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer,
            httpClient = layui.httpClient,
            miniTab = layui.miniTab;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '/stu/list',
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.count, //解析数据长度
                    "data": res.data.rows //解析数据列表
                };
            },
            response: {
                statusName: 'code',
                statusCode: 200
            },
            cellMinWidth: 60,
            cols: [[
                {field: 'sid', width: 120, title: '学号', sort: true},
                {field: 'name', width: 150, title: '姓名'},
                {field: 'avatar', width:120, title: '照片', templet: '#imgTpl' },
                {field: 'gender', width:100, title: '性别', templet: function(d) {
                        if (d.gender == true) {
                            return '<div><span class="layui-badge layui-bg-green">男</span></div>'
                        } else {
                            return '<div><span class="layui-badge layui-bg-danger">女</span></div>'
                        }
                    }},
                {field: 'enrollDate', width: 150, title: '入学时间', sort: true},
                {field: 'major', width: 150, title: '专业', templet: '<div>{{d.major.name}}</div>'},
                {title: '操作', minWidth: 150, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limit: 10,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // 执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    name: data.field.name,
                    gender: data.field.gender
                }
            }, 'data');
            return false;
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                miniTab.openNewTabByIframe({
                    href: '/stu/edit/' + data.id,
                    title: '编辑学生信息'
                });
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该学生?', function (index) {
                    layer.close(index);

                    httpClient.delete('/stu/del/'+ data.sid, {}, (response) => {
                        tableIns.reload();
                    }, (response) => {
                        tableIns.reload();
                    });

                });
            }
        });
    });
</script>
<script type="text/html" id="imgTpl">
    {{#  if(d.avatar==null ){ }}
        <img src="/avatar.jpg">
    {{#  } else { }}
        <img src="{{ d.avatar }}">
    {{#  } }}
</script>
</body>
</html>